package com.flash.SystemManagement.controller;


import com.flash.SystemManagement.entity.User;
import com.flash.SystemManagement.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {
    @Resource
    UserService userService;

    @RequestMapping("toUser")
    public String toUser(){
        System.out.println(1);
        return "User";
    }
    @RequestMapping("/selectUser")
    @ResponseBody
    public Map selectUser(){
        System.out.println("这是查询测试");
        HashMap map = new HashMap();
        List<User> plates = userService.selectUser();
        System.out.println(plates);
        map.put("code","0");
        map.put("msg","");
        map.put("count",0);
        map.put("data",plates);
        return map;
    }

    @RequestMapping("/insertUsers")
    @ResponseBody
    public String insertUsers(User user){
        System.out.println("添加测试用户" + user);
        int i = userService.insertUsers(user);
        System.out.println(i);
        if (i>0){
            return "ok";
        }else {
            return "no";
        }
    }

    @ResponseBody
    @RequestMapping("deleteUsers")
    public String deleteUsers(int userId){
        System.out.println("删除服务类:"+userId);
        int i = userService.deleteUsers(userId);
        if (i != 0){
            return "yes";
        }else {
            return "no";
        }
    }

    /**
     * 根据用户名称来查找
     * @param userName
     * @return
     */
    @ResponseBody
    @RequestMapping("/selectLikeUser")
    public Map selectLikeUser(String userName, int page, int limit){
        System.out.println("模糊查询:"+userName);
        List<User> users = userService.selectLikeUser(userName,  page, limit);
        Map hashMap = new HashMap();
        hashMap.put("code","0");
        hashMap.put("msg","");
        hashMap.put("count",5);
        hashMap.put("data",users);
        System.out.println(users);
        return hashMap;
    }

    @ResponseBody
    @RequestMapping("/updateUser")
    public String updateUser(User user){
        System.out.println(user);
        int i = userService.updateUser(user);
        if (i!=0){
            return "yes";
        }else {
            return "no";
        }

    }


}
